package com.gozap.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyFile {

	public static void copyFileWithNio(String source, String dest, boolean append)
			throws IOException {
		FileInputStream input = new FileInputStream(source);
		FileOutputStream output = new FileOutputStream(dest);

		FileChannel readChannel = input.getChannel();
		FileChannel writeChannel = output.getChannel();

		ByteBuffer buffer = ByteBuffer.allocate(1024);
		boolean isFinsh = false;

		while (!isFinsh) {

			buffer.clear();
			int size = readChannel.read(buffer);
			if (size == -1) {
				isFinsh = true;
				continue;
			}
			buffer.flip();
			writeChannel.write(buffer);
		}
	}
}
